package com.yeyks.common.utils;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.system.SystemUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;
import java.util.StringJoiner;

/*
打印工程目录的堆栈的工具类
 */
@Slf4j
public class SystemStrackUtil {

    public static void logYyksStack() {
        logYyksExceptionStack(null);
    }

    public static void logYyksExceptionStack(Throwable throwable) {
        try {
            StackTraceElement[] stackTrace;
            if (Objects.nonNull(throwable)) {
                log.info(ExceptionUtil.getMessage(throwable));
                stackTrace = throwable.getStackTrace();
            } else {
                stackTrace = Thread.currentThread().getStackTrace();
            }
            StringJoiner stringJoiner = new StringJoiner(SystemUtil.get(SystemUtil.LINE_SEPRATOR));
            for (StackTraceElement stackTraceElement : stackTrace) {
                String stackTraceStr = stackTraceElement.toString();
                if (stackTraceStr.indexOf("com.yeyks") < 0) {
                    continue;
                }
                stringJoiner.add(stackTraceStr);
            }
            log.info(stringJoiner.toString());
        } catch (Exception e) {
        }
    }

}
